/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.samples.servlet;

import java.util.Set;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import io.sentry.Sentry;

/**
 * SentryInitializer
 *
 * @since 2021-05-24
 */
public class SentryInitializer implements ServletContainerInitializer {
    @Override
    public void onStartup(Set<Class<?>> classes, ServletContext ctx) throws ServletException {
        Sentry.init(
            options -> {
                // NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in
                // your Sentry project/dashboard
                options.setDsn(
                    "https://502f25099c204a2fbf4cb16edc5975d1@o447951.ingest.sentry.io/5428563");

                // All events get assigned to the release. See more at
                // https://docs.sentry.io/workflow/releases/
                options.setRelease("io.sentry.samples.servlet@3.0.0+1");

                // Modifications to event before it goes out. Could replace the event altogether
                options.setBeforeSend(
                    (event, hint) -> {
                        // Drop an event altogether:
                        if (event.getTag("SomeTag") != null) {
                            return null;
                        }
                        return event;
                    });

                // Allows inspecting and modifying, returning a new or simply rejecting (returning null)
                options.setBeforeBreadcrumb(
                    (breadcrumb, hint) -> {
                        // Don't add breadcrumbs with message containing:
                        if (breadcrumb.getMessage() != null
                            && breadcrumb.getMessage().contains("bad breadcrumb")) {
                            return null;
                        }
                        return breadcrumb;
                    });

                // Configure the background worker which sends events to sentry:
                // Wait up to 5 seconds before shutdown while there are events to send.
                options.setShutdownTimeout(5000);

                // Enable SDK logging with Debug level
                options.setDebug(true);

                // Include frames from our package
                options.addInAppInclude("io.sentry.samples");
            });
    }
}
